package com.gamesa.osb.reports.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import com.gamensa.osb.reports.model.OSBData;
import com.gamensa.osb.reports.model.ReportOSB;
import com.gamesa.osb.reports.dao.ReportOSBDao;
import com.gamesa.osb.reports.utils.Constants;
import com.gamesa.osb.reports.utils.DataOSBMapper;
import com.gamesa.osb.reports.utils.ReportOSBMapper;

public class ReportOSBDaoImpl implements ReportOSBDao{
	
	private DataSource datasource;  
	private JdbcTemplate jdbcTemplate;
		
	public ReportOSBDaoImpl(){
		
	}
	
	@Override
	public List<ReportOSB> getServices(String application, Date from, Date to) {

		List<ReportOSB> listado = new ArrayList<ReportOSB>();
		
	    try{
	    	
//	    	application = "%codClientApp=%" + application + "%" + "codProxyService=%";
//	    	application = "%" + Constants.APPLICATION_CODE + "%" + application + "%" + Constants.SERVICE_CODE + "%";
	    	
		    listado =  jdbcTemplate
		    .query(Constants.FIND_PROXY_SERVICES_QUERY, 
		    		new Object[] {from, to, application}, 
		    		new ReportOSBMapper());
	 	
	    } catch(DataAccessException e){
	    	e.printStackTrace();
	    } catch (Exception e){
	    	e.printStackTrace();
	    }
	    return listado;
	}

	@Override
	public List<ReportOSB> getApplications(String service, Date from, Date to) {
		
		List<ReportOSB> listado = new ArrayList<ReportOSB>();
		
		try {
			
//			service = "%codProxyService=%" + service + "%";
//			service = "%" + Constants.SERVICE_CODE + "%" + service + "%";

		    listado =  jdbcTemplate
		    .query(Constants.FIND_APPLICATION_CLIENT_QUERY, 
		    		new Object[] { from, to, service, service}, 
		    		new ReportOSBMapper());
	 	
		    } catch(DataAccessException e){
		    	e.printStackTrace();
		    } catch (Exception e){
		    	e.printStackTrace();
		    }
		 
	    return listado;
	}
//
//	@Override
//	public List<OSBData> getServicesData(String application, Date from, Date to) {
//
//		List<ReportOSB> listAllServices = getApplications(application, from, to);
//		
//		List<ReportOSB> list = new ArrayList<ReportOSB>();
//		
//		list.addAll(listAllServices);
//		
//		List<OSBData> resultList = new ArrayList<OSBData>();
//		
//		for(ReportOSB report:list){
//			OSBData data = new OSBData();
//			data.setAccess(2);
//			data.setApplication(report.getMsgLables());
//			data.setService(report.getInboundServiceURI());
//			
//			resultList.add(data);
//		}
//				
//		return resultList;
//	}
//	
//	@Override
//	public List<OSBData> getApplicationsData(String service, Date from, Date to) {
//
//		List<ReportOSB> listAllApps = getServices(service, from, to);
//		
//		List<OSBData> resultList = new ArrayList<OSBData>();
//
//		List<ReportOSB> list = new ArrayList<ReportOSB>();
//		
//		list.addAll(listAllApps);
//		
////		// TODO Hay que completar bien el data.
//		for(ReportOSB report:list){
//			OSBData data = new OSBData();
//			data.setAccess(2);
//			data.setApplication(report.getMsgLables());
//			data.setService(report.getInboundServiceURI());
//			
//			resultList.add(data);
//		}
//				
//		return resultList;
//	}


	@Override
	public List<OSBData> getServicesData(String application, Date from, Date to) {

		if (from == null){
			from = new Date(0);
		}
		if (to == null){
			to = new Date();
		}
		List<OSBData> listado = null;
		try{
			listado =  jdbcTemplate
		    .query(Constants.APPLICATION_CLIENT_QUERY, 
		    		new Object[] { from, to, application, application}, 
		    		new DataOSBMapper());
 	
	    } catch(DataAccessException e){
	    	e.printStackTrace();
	    } catch (Exception e){
	    	e.printStackTrace();
	    }
		return listado;
	}

	@Override
	public List<OSBData> getApplicationsData(String service, Date from, Date to) {
		List<OSBData> listado = null;

		if (from == null){
			from = new Date(0);
		}
		if (to == null){
			to = new Date();
		}
		try{
			listado =  jdbcTemplate
		    .query(Constants.PROXY_SERVICE_QUERY, 
		    		new Object[] { from, to, service}, 
		    		new DataOSBMapper());
 	
	    } catch(DataAccessException e){
	    	e.printStackTrace();
	    } catch (Exception e){
	    	e.printStackTrace();
	    }
		return listado;
	}
	
	public DataSource getDatasource() {
		return datasource;
	}

	public void setDatasource(DataSource datasource) {
		this.datasource = datasource;     
		this.jdbcTemplate = new JdbcTemplate(datasource);
	}

	private void verifyDates(Date from, Date to){
		if (from == null){
			from = new Date(0);
		}
		if (to == null){
			to = new Date();
		}
	}

}
